package com.zx.mes.rabbit.transaction.hyl.controller;

import com.zx.mes.rabbit.transaction.hyl.annotation.MqSender;
import com.zx.mes.rabbit.transaction.hyl.model.User;
import com.zx.mes.rabbit.transaction.hyl.sender.RabbitSender;
import com.zx.mes.rabbit.transaction.hyl.util.MQConstants;
import com.zx.mes.rabbit.transaction.hyl.util.RabbitMetaMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 对分布式事务进行测试
 * @author hyl
 * @date 2018-3-20
 * @version 1.0.0
 */
@RestController
public class MessageProviderController {

    @Autowired
    private RabbitSender rabbitmqSender;


    @GetMapping("/sender")
    @Transactional(rollbackFor = Exception.class)
    public String sender(){
        /* 生成一个发送对象 */
        RabbitMetaMessage rabbitMetaMessage = new RabbitMetaMessage();
        /* 设置交换机 */
        rabbitMetaMessage.setExchange(MQConstants.BUSINESS_EXCHANGE);

        /* 指定routing key */
        rabbitMetaMessage.setRoutingKey(MQConstants.BUSINESS_KEY);

        User user = new User();
        user.setAge(27);
        user.setName("神一般的存在");
        Map<String,User> map =new HashMap<>();

        User user2 = new User();
        user2.setAge(28);
        user2.setName("神一般的存在2");

        User user3 = new User();
        user3.setAge(29);
        user3.setName("神一般的存在3");

        map.put("xx2", user3);
        map.put("xx3", user3);
        user.setMap(map);

        /* 设置需要传送的消息体，可以是任意对象 */
        //rabbitMetaMessage.setPayload("你想传递的消息");
        rabbitMetaMessage.setPayload(user);

        try {
            rabbitmqSender.send(rabbitMetaMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }


    @GetMapping("/annotation")
    @MqSender(exchange=MQConstants.BUSINESS_EXCHANGE,routingkey=MQConstants.BUSINESS_KEY)
    public RabbitMetaMessage senderA(String cc){
        /* 生成一个发送对象 */
        RabbitMetaMessage rabbitMetaMessage = new RabbitMetaMessage();
        /* 设置交换机 */
        rabbitMetaMessage.setExchange(MQConstants.BUSINESS_EXCHANGE);

        /* 指定routing key */
        rabbitMetaMessage.setRoutingKey(MQConstants.BUSINESS_KEY);

        User user = new User();
        user.setAge(27);
        user.setName("神一般的存在");
        Map<String,User> map =new HashMap<>();

        User user2 = new User();
        user2.setAge(28);
        user2.setName("神一般的存在2");

        User user3 = new User();
        user3.setAge(29);
        user3.setName("神一般的存在3");

        map.put("xx2", user3);
        map.put("xx3", user3);
        user.setMap(map);

        /* 设置需要传送的消息体，可以是任意对象 */
        rabbitMetaMessage.setPayload(user);

        return rabbitMetaMessage;
    }

    @RequestMapping(path = "/xxx",name = "xccc")
    public String say(){
        return "xx";
    }
}
